package com.lanlian.cayman.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioGroup;

import com.lanlian.cayman.R;
import com.lanlian.cayman.adapter.InformationBannerAdapter;
import com.lanlian.cayman.utils.VolleyRequestUtils;
import com.lanlian.commonlib.base.BaseFragment;
import com.lanlian.commonlib.bean.BannerInfo;
import com.lanlian.commonlib.bean.BaseResponse;
import com.lanlian.commonlib.bean.ReqBanner;
import com.lanlian.commonlib.bean.RespBanner;
import com.lanlian.commonlib.common.SystemConfig;
import com.lanlian.commonlib.network.INetWorkCallback;
import com.lanlian.uikit.banner.AutoScrollViewPager;
import com.lanlian.utillib.DisplayUtil;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.buildins.circlenavigator.CircleNavigator;

import java.util.ArrayList;

/**
 * <pre>
 *     author : huxiaobo
 *     e-mail : xiaobo.hu@foxtry.com
 *     date   : 2018/8/9 13:40
 *     desc   : 资讯TAB
 *     version: 1.0
 * </pre>
 */
public class InformationFragment extends BaseFragment {

    private AutoScrollViewPager mViewPagerBanner;
    private RadioGroup mRgInformation;
    private ViewPager mViewPager;
    private MagicIndicator mMagicIndicator;

    private ArrayList<Fragment> mFragments = new ArrayList<>();
    private ArrayList<BannerInfo> mImgList = new ArrayList<>();

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_information, null);
        initView(view);
        setListeners();
        initFragment();
        mRgInformation.check(R.id.rb_lion_analysis);
        return view;
    }

    @Override
    protected void initView(View view) {
        super.initView(view);
        mViewPagerBanner = view.findViewById(R.id.view_pager_banner);
        mRgInformation = view.findViewById(R.id.rg_information);
        mViewPager = view.findViewById(R.id.view_pager);
        mMagicIndicator = view.findViewById(R.id.magic_indicator);
        ViewGroup.LayoutParams layoutParams = mViewPagerBanner.getLayoutParams();
        layoutParams.height = (int) (DisplayUtil.getScreenWidth(getActivity()) * 0.4);
        mViewPagerBanner.setLayoutParams(layoutParams);
    }

    @Override
    protected void setListeners() {
        super.setListeners();
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {
            }

            @Override
            public void onPageSelected(int i) {
                switch (i) {
                    case 0:
                        mRgInformation.check(R.id.rb_lion_analysis);
                        break;
                    case 1:
                        mRgInformation.check(R.id.rb_newsflash);
                        break;
                    default:
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });

        mRgInformation.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_lion_analysis:
                        mViewPager.setCurrentItem(0);
                        break;
                    case R.id.rb_newsflash:
                        mViewPager.setCurrentItem(1);
                        break;
                    default:
                        break;
                }
            }
        });
    }

    private void initFragment() {
        mFragments.add(new LionAnalysisFragment(new LionAnalysisFragment.OnRefreshBanner() {
            @Override
            public void onRefresh() {
                getBanner();
            }
        }));
        mFragments.add(new NewsflashFragment(new NewsflashFragment.OnRefreshBanner() {
            @Override
            public void onRefresh() {
                getBanner();
            }
        }));
        mViewPager.setAdapter(new FragmentPagerAdapter(getChildFragmentManager()) {
            @Override
            public Fragment getItem(int i) {
                return mFragments.get(i);
            }

            @Override
            public int getCount() {
                return mFragments.size();
            }
        });
    }

    public void getBanner() {
        VolleyRequestUtils.getInstance().getBanner(this.toString(), new ReqBanner(), new INetWorkCallback() {
            @Override
            public void onSuccess(Object response) {
                RespBanner respBanner = (RespBanner) response;
                mImgList = respBanner.getData();
                if (mImgList.size() == 0) {
                    mViewPagerBanner.setVisibility(View.GONE);
                } else {
                    mViewPagerBanner.setVisibility(View.VISIBLE);
                    setRadio(mImgList.size());
                    initBanner();
                }
            }

            @Override
            public void onFail(BaseResponse baseResponse) {
                mViewPagerBanner.setVisibility(View.GONE);
            }
        });
    }

    private void initBanner() {
        mViewPagerBanner.setAdapter(new InformationBannerAdapter(getActivity(), mImgList));
        mViewPagerBanner.startAutoScroll();
        mViewPagerBanner.setInterval(3000);
        mViewPagerBanner.setCurrentItem(10 * mImgList.size());
    }

    private void setRadio(int size) {
        CircleNavigator circleNavigator = new CircleNavigator(getActivity());
        circleNavigator.setCircleCount(size);
        circleNavigator.setCircleColor(Color.WHITE);
        circleNavigator.setCircleClickListener(new CircleNavigator.OnCircleClickListener() {
            @Override
            public void onClick(int index) {
                mViewPagerBanner.setCurrentItem(mViewPagerBanner.getCurrentItem() / size * size + index);
            }
        });
        mMagicIndicator.setNavigator(circleNavigator);
        mViewPagerBanner.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                mMagicIndicator.onPageScrolled(position % size, positionOffset, positionOffsetPixels);
            }

            @Override
            public void onPageSelected(int position) {
                mMagicIndicator.onPageSelected(position % size);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                mMagicIndicator.onPageScrollStateChanged(state);
            }
        });

    }

}
